package com.wannengbang.cloudleader.mine.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.bean.OrderListBean;
import com.wannengbang.cloudleader.bean.UserInfoBean;
import com.wannengbang.cloudleader.utils.DateTimeUtil;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;
import com.wannengbang.cloudleader.utils.SPManager;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 订单列表
 */
public class ByOrderListAdapter extends RecyclerView.Adapter<ByOrderListAdapter.ViewHolder> {

    private List<OrderListBean.DataBean.ItemListBean> listsBeans;
    private Context context;
    private UserInfoBean userInfoBean;

    public ByOrderListAdapter(List<OrderListBean.DataBean.ItemListBean> listsBeans) {
        this.listsBeans = listsBeans;
        userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
    }

    private OnCallBackListener onCallBackListener;

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_by_my_order_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        OrderListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        holder.tvBtnDelivery.setVisibility(View.GONE);
        holder.tvPrepareGoods.setVisibility(View.GONE);
        holder.tvTurnSuperior.setVisibility(View.GONE);
        if (dataBean.getOrder_status() == 1) {
            holder.tvOrderStatus.setText("等待(" + dataBean.getAgent_name() + ")付款");
        } else if (dataBean.getOrder_status() == 2) {
            holder.tvOrderStatus.setText("待发货给(" + dataBean.getAgent_name() + ")");
            holder.tvBtnDelivery.setVisibility(View.VISIBLE);
            holder.tvPrepareGoods.setVisibility(View.VISIBLE);
            if (userInfoBean.getData().getId() != 1) {
                holder.tvTurnSuperior.setVisibility(View.VISIBLE);
            }
        } else if (dataBean.getOrder_status() == 3) {
            holder.tvOrderStatus.setText("等待(" + dataBean.getAgent_name() + ")收货");
            //总账户
            if (userInfoBean.getData().getId() == 1) {
                if (dataBean.getShop_goods_count() - dataBean.getDelivery_goods_count() > 0) {
                    holder.tvPrepareGoods.setVisibility(View.VISIBLE);
                }
            }
        } else if (dataBean.getOrder_status() == 4) {
            holder.tvOrderStatus.setText("已完成(" + dataBean.getAgent_name() + ")");
            if (userInfoBean.getData().getId() == 1) {
                if (dataBean.getShop_goods_count() - dataBean.getDelivery_goods_count() > 0) {
                    holder.tvPrepareGoods.setVisibility(View.VISIBLE);
                }
            }
        } else if (dataBean.getOrder_status() == 9) {
            holder.tvOrderStatus.setText("已取消(" + dataBean.getAgent_name() + ")");
        }
        if (dataBean.getDelivery_type() == 1) {
            holder.tvOrderType.setText("订单类型：线上付款");
        } else if (dataBean.getDelivery_type() == 2) {
            holder.tvOrderType.setText("订单类型：线下付款");
        } else {
            holder.tvOrderType.setText("");
        }
        String totalPrice = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getOrder_fee()) / 100);
        holder.tvTotalPrice.setText(totalPrice);

        holder.tvOrderNo.setText("订单编号：" + dataBean.getId());
        holder.tvCreateTime.setText("下单时间：" + DateTimeUtil.format7(new Date(Long.parseLong(dataBean.getCreate_time()) * 1000)));

        OrderProductListAdapter listAdapter = new OrderProductListAdapter(dataBean.getShop_order_goods());
        holder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
        holder.recyclerView.setAdapter(listAdapter);

        holder.tvBtnDelivery.setOnClickListener(v -> {
            onCallBackListener.onCallBack(1, position);
        });
        holder.tvPrepareGoods.setOnClickListener(v -> {
            onCallBackListener.onCallBack(2, position);
        });
        holder.tvTurnSuperior.setOnClickListener(v -> {
            onCallBackListener.onCallBack(4, position);
        });

        holder.recyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return holder.itemView.onTouchEvent(event);
            }
        });
        holder.itemView.setOnClickListener(v -> {
            onCallBackListener.onCallBack(3, position);
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_order_status)
        TextView tvOrderStatus;
        @BindView(R.id.recyclerView)
        RecyclerView recyclerView;
        @BindView(R.id.tv_create_time)
        TextView tvCreateTime;
        @BindView(R.id.tv_order_no)
        TextView tvOrderNo;
        @BindView(R.id.tv_order_type)
        TextView tvOrderType;
        @BindView(R.id.tv_total_price)
        TextView tvTotalPrice;
        @BindView(R.id.tv_turn_superior)
        TextView tvTurnSuperior;
        @BindView(R.id.tv_prepare_goods)
        TextView tvPrepareGoods;
        @BindView(R.id.tv_btn_delivery)
        TextView tvBtnDelivery;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnCallBackListener {
        void onCallBack(int type, int position);
    }
}
